package top.twoteam.equipment.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.twoteam.equipment.entity.EquipmentInfo;
import top.twoteam.equipment.service.EquipmentService;
import top.twoteam.equipment.utils.PageRequest;
import top.twoteam.equipment.utils.Result;
import top.twoteam.equipment.utils.StatusCode;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author binghan
 * @version 1.0
 * @description: TODO 设备信息
 * @date 2022/6/2 17:10
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    EquipmentService equipmentService;

    /**
     * 条件分页查询
     * @param req
     * @return
     */
    @PostMapping("/queryByCondition")
    public Result<PageInfo<EquipmentInfo>> QueryByCondition(@RequestBody PageRequest<EquipmentInfo> req) {
        // 返回结果信息
        Result<PageInfo<EquipmentInfo>> res = new Result<>();
        try {
            // 获取返回PageInfo
            PageInfo<EquipmentInfo> equipmentInfoPageInfo = equipmentService.queryByCondition(req);
            //  返回数据成功
            res.setSuccess();
            // 返回数据信息
            res.setData(equipmentInfoPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData(null);
        }
        return res;

    }

    /**
     * 主键批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteEquimentInfo")
    public  Result  deleteEquimentInfo(@RequestBody List<Integer> ids){
        // 返回结果信息
        Result res = new Result<>();
        try {
            equipmentService.deleteEquimentInfo(ids);
            //  返回数据成功
            res.setSuccess();
            res.setData(null);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData(null);
        }
        return res;
    }

    /**
     * 修改
     * @param equipmentInfo
     * @return
     */
    @PutMapping("/updateEquipmentInfo")
    public  Result  updateEquipmentInfo(@RequestBody EquipmentInfo equipmentInfo){
        // 返回结果信息
        Result res = new Result<>();
        try {
            equipmentService.updateEquipmentInfo(equipmentInfo);
            //  返回数据成功
            res.setSuccess();
            res.setData(null);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData(null);
        }
        return res;
    }

    /**
     * 插入
     * @param equipmentInfo
     * @return
     */
    @PostMapping("/insertEquipmentInfo")
    public  Result  insertEquipmentInfo(@RequestBody EquipmentInfo equipmentInfo){
        // 返回结果信息
        Result res = new Result<>();

        try {
            equipmentService.insertEquipmentInfo(equipmentInfo);
            //  返回数据成功
            res.setSuccess();
            res.setData(null);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData(null);
        }
        return res;
    }


    /**
     * 下载设备信息Excel文件
     * @param response
     * @return
     */
    @PostMapping("/downloadEquipmentInfoExcel")
    public void downloadEquipmentInfoExcel(HttpServletResponse response) {

        // 返回结果信息
        Result res = new Result<>();
        // 获取所有设备信息
        List<EquipmentInfo> equipmentInfoList = equipmentService.queryAllEquipmentInfos();

        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("设备信息表", "UTF-8");
            // 先暴露Content-Disposition 在设置
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), EquipmentInfo.class).autoCloseStream(Boolean.FALSE).sheet("设备信息").doWrite(equipmentInfoList);
        } catch (IOException e) {
        e.printStackTrace();
        System.out.println("下载失败");
    }
}


}
